package top.lingkang.sprites;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import top.lingkang.CoolRunGame;
import top.lingkang.entity.GroundBody;
import top.lingkang.screen.PlayScreen;
import top.lingkang.utils.BodyUtils;

public class GroundSprite extends Sprite {
    private PlayScreen playScreen;

    private World world;
    public Body body;
    private Array<GroundBody> groundBodyArray = new Array<>();

    public GroundSprite(PlayScreen playScreen, World world) {
        this.playScreen = playScreen;
        this.world = world;

        //body = BodyUtils.createGroundBody(0, 0, 50, 100 / CoolRunGame.PPM, world);
        GroundBody groundBody = new GroundBody();
        groundBody.x = 0;
        groundBody.createX = 0;
        groundBody.destroyX = 60;
        groundBody.width = 50;
        groundBody.height = 100 / CoolRunGame.PPM;
        groundBodyArray.add(groundBody);

        GroundBody groundBody1 = new GroundBody();
        groundBody1.x = 61f;
        groundBody1.createX = 30;
        groundBody1.destroyX = 71;
        groundBody1.width = 10;
        groundBody1.height = 200 / CoolRunGame.PPM;
        groundBodyArray.add(groundBody1);
    }

    boolean add;

    public void update(float delta) {
        //batch.draw(animation.getKeyFrame(stateTime, true), getX(), getY());
        if (!add && playScreen.playerSprite.position.x > 20) {
            add = true;
            BodyUtils.createGroundBody(72, 0, 20, 200 / CoolRunGame.PPM, world);
        } else {

        }

        int size = groundBodyArray.size;
        for (; --size >= 0; ) {
            GroundBody groundBody = groundBodyArray.get(size);
            // 判断移除
            if (playScreen.playerSprite.position.x > groundBody.destroyX) {
                world.destroyBody(groundBody.body);
                groundBodyArray.removeIndex(size);
                continue;
            }

            if (groundBody.isExist){
                // 绘制
                
            }

            // 判断创建
            if (!groundBody.isExist && playScreen.playerSprite.position.x > groundBody.createX) {
                groundBody.isExist = true;
                groundBody.body = BodyUtils.createGroundBody(groundBody.x, 0, groundBody.width, groundBody.height, world);
            }
        }
    }

    public void dispose() {

    }
}
